﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using LTP.Accounts.Bus;
using System.Configuration;
using System.Collections.Generic;
namespace Maticsoft.Web.Accounts
{
	/// <summary>
	/// usermodify 的摘要说明。
	/// </summary>
	public partial class usermodify : System.Web.UI.Page
	{	
		protected void Page_Load(object sender, System.EventArgs e)
		{
            if (!Page.IsPostBack)
            {
                BindRoleData();
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int UsersId = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(UsersId);
                }
            }
		}
        private void BindRoleData()
        {
            ChinaSoft.Model.Model_tb_MidSoft_Roles roles = new ChinaSoft.Model.Model_tb_MidSoft_Roles();
            ChinaSoft.BLL.BLL_tb_MidSoft_Roles rolesMgr = new ChinaSoft.BLL.BLL_tb_MidSoft_Roles();
            ChinaSoft.Common.BindControl.BindDropDownList(ddlRole, rolesMgr.GetAllList(), "RoleName", "RolesId");
        }

        private void ShowInfo(int UsersId)
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            ChinaSoft.Model.Model_tb_MidSoft_Users model = bll.GetModel(UsersId);
            this.lblUserAccount.Text = model.UserAccount;
            this.txtUserName.Text = model.UserName;
            this.imgHeader.ImageUrl = "/Uploads/"+model.UserImgFolder+"/"+model.UserImgPath;
            this.txtCompany.Text = model.Company;
            this.txtDepartMent.Text = model.DepartMent;
            if (model.Sex == "男")
            {
                this.rbtnMen.Checked = true;
                this.rbtnWomen.Checked = false;
            }
            else
            {
                this.rbtnMen.Checked = false;
                this.rbtnWomen.Checked = true;
            }
            this.txtBirthDay.Value = ChinaSoft.Common.Input.SetString(model.BirthDay)==""?"":Convert.ToDateTime(model.BirthDay).ToShortDateString();
            this.txtTel.Text = model.Tel;
            this.txtDuty.Text = model.Duty;
            if (model.State == 0)
                chboxKaiTong.Checked = true;
            ChinaSoft.BLL.BLL_tb_MidSoft_UserHaveRoles bllUserRoles = new ChinaSoft.BLL.BLL_tb_MidSoft_UserHaveRoles();
            ChinaSoft.Model.Model_tb_MidSoft_UserHaveRoles modelUserRoles = new ChinaSoft.Model.Model_tb_MidSoft_UserHaveRoles();
            List<ChinaSoft.Model.Model_tb_MidSoft_UserHaveRoles> listUserRoles = bllUserRoles.GetModelList("UsersId="+UsersId+"");
            if (listUserRoles != null && listUserRoles.Count > 0 && listUserRoles[0].RolesId > 0)
            {
                ddlRole.SelectedValue = listUserRoles[0].RolesId.ToString();
            }
        }

        public void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtUserName.Text.Trim().Length == 0)
            {
                strErr += "用户名不能为空！\\n";
            }
            if (this.txtCompany.Text.Trim().Length == 0)
            {
                strErr += "单位名不能为空！\\n";
            }
            if (this.txtDepartMent.Text.Trim().Length == 0)
            {
                strErr += "部门不能为空！\\n";
            }
            if (!Maticsoft.Common.PageValidate.IsDateTime(txtBirthDay.Value))
            {
                strErr += "出生日期格式错误！\\n";
            }
            if (this.txtTel.Text.Trim().Length == 0)
            {
                strErr += "联系电话不能为空！\\n";
            }
            if (!Maticsoft.Common.PageValidate.IsPhone(txtTel.Text.Trim()))
            {
                strErr += "电话号码只能是数字！\\n";
            }
            if (this.txtDuty.Text.Trim().Length == 0)
            {
                strErr += "职务不能为空！\\n";
            }


            if (strErr != "")
            {
                Maticsoft.Common.MessageBox.Show(this, strErr);
                return;
            }
            int UsersId = int.Parse(Request.Params["id"]);
            string UserName = this.txtUserName.Text;

            #region 图片上传
            string UserImgFolder = "";
            string UserImgPath = "";
            if (FileUpload1.HasFile)
            {
                string result = ChinaSoft.Common.UpFile.UploadFile(FileUpload1, Server.MapPath("/Uploads"), "", 5);
                if (result.Contains("错误"))
                {
                    Maticsoft.Common.MessageBox.Show(this, result);
                    return;
                }
                else
                {
                    UserImgFolder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
                    UserImgPath = result;
                }
            }
            #endregion
            string Company = this.txtCompany.Text;
            string DepartMent = this.txtDepartMent.Text;
            string Sex = this.rbtnMen.Checked==true?"男":"女";
            DateTime BirthDay = DateTime.Parse(this.txtBirthDay.Value);
            string Tel = this.txtTel.Text;
            string Duty = this.txtDuty.Text;
            int State = chboxKaiTong.Checked==true?0:1;


            ChinaSoft.BLL.BLL_tb_MidSoft_Users bll = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            ChinaSoft.Model.Model_tb_MidSoft_Users model = bll.GetModel(UsersId);
            model.UserName = UserName;
            if (UserImgFolder != "" && UserImgPath != "")
            {
                model.UserImgFolder = UserImgFolder;
                model.UserImgPath = UserImgPath;
            }
            model.Company = Company;
            model.DepartMent = DepartMent;
            model.Sex = Sex;
            model.BirthDay = BirthDay;
            model.Tel = Tel;
            model.Duty = Duty;
            model.State = State;

            bll.Update(model);
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "userinfo.aspx?id="+UsersId);
        }

        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("userlist.aspx");
        }
	}
}
